package com.moleq.adapter;

import java.util.List;
import com.moleq.models.FreeFunction;
import com.moleq.mgpos.activities.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class FreeFuncAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<FreeFunction> listFreeFunc;
	private int selectedRow = 0;
	private Context shareContext;
	
	public FreeFuncAdapter(Context context,List<FreeFunction> listFreeFunc){
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listFreeFunc = listFreeFunc;
	}
	
	public void setSource(List<FreeFunction> listFreeFunc){
		this.listFreeFunc = listFreeFunc;
	}
	
	public int getCount() {
		return listFreeFunc.size();
	}

	public Object getItem(int position) {
		return listFreeFunc.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder= null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.func_list_item, null);
			holder.tv_freefunc_id = (TextView) convertView.findViewById(R.id.tv_freefunc_id);
			holder.tv_freefunc_name = (TextView) convertView.findViewById(R.id.tv_freefunc_name);
			
			holder.tv_freefunc_id.setText(String.valueOf(listFreeFunc.get(position).getFf_id()));
			holder.tv_freefunc_name.setText(listFreeFunc.get(position).getFf_name());
			convertView.setTag(holder);
//		}else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		
		if (position == selectedRow) {
			holder.tv_freefunc_id.setBackgroundColor(Color.rgb(255,219,16));
			holder.tv_freefunc_name.setBackgroundColor(Color.rgb(255,219,16));
		} else {
			holder.tv_freefunc_id.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_freefunc_name.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}
	
	public final class ViewHolder{
		public TextView tv_freefunc_id;
		public TextView tv_freefunc_name;
	}
	
	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}
}
